package com.zixuan.sina.controller;
import com.zixuan.sina.entity.User;
import com.zixuan.sina.service.UserService;
import com.zixuan.sina.utils.Result;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/api/user/")
public class SysUserController {
    @Resource
    UserService userService;

    @PostMapping("login")
    public User login(@RequestParam("username") String username) {
        return userService.findUserByUsername(username);
    }

    //获取当前登录用户
    @GetMapping("getLoginUser")
    public Result getLoginUser() {
        return userService.getInfo();
    }

    //登出
    @GetMapping("logout")
    public Result logout(HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            token = request.getParameter("token");
            if (token == null) {
                throw new RuntimeException("未检测到token信息");
            }
        }
        return userService.logout(token);
    }
    //todo:添加用户
    @PostMapping("add")
    public Result add(@RequestBody User user){
        return userService.addUser(user);
    }
    //根据id查询用户信息
    @GetMapping("getUserInfoBy")
    public Result getUserInfoById(@RequestParam("id") String id,@RequestParam("isSpecial") int isSpecial,@RequestParam("type") String type) {//postman查不到这个接口
        return userService.getUserInfoById(id,isSpecial,type);
    }


    @PostMapping("/updateById")
    public Result updateById(@RequestBody User user){
        return userService.editUserInfo(user);
    }

    @GetMapping("/getUserById/{id}")
    public Result getUserById(@PathVariable("id") String id){
        return userService.getUserById(id);
    }
    //获取用户列表
    /**
     * 根据关键字获取用户列表
     * @param key
     * @return
     */
    @PostMapping("/getUserList")
    public Result getUserList(@RequestBody String key){
        return userService.getUserList(key);
    }

}
